<?php

namespace App\Repositories;

use App\Exceptions\GeneralException;
use App\Models\Courier;
use Illuminate\Support\Facades\Redis;

class CourierRepository extends BaseRepository
{
    const REDIS_EXPIRES_FOR_DAY = 60;

    public function model()
    {
        return Courier::class;
    }

    /**
     * 获取快递员列表 $service_id, $limit, $name, $start_time, $end_time
     * Date: 2018/05/10
     * @param $service_id
     * @param $limit
     * @param $name
     * @param $start_time
     * @param $created_at
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getCouriers($service_id, $limit = false, $name = false, $created_at = false)
    {
        $query = \DB::table('courier as c');
        if ($service_id !== false) {
            $query->where('c.service_id', '=', $service_id);
        }
        if ($name !== false) {
            $query->where('c.name', 'like', '%' . $name . '%');
        }
        if ($created_at != false) {
            $query->whereBetween('c.created_at', $created_at);
        }
        $query->select(['c.id', 'c.name', 'c.phone', 'c.created_at', 'c.jurisdiction', 'c.is_authentication', 'c.delivery_num', 'c.send_num', 's.designation as service_name'])
            ->leftJoin('service as s', 'c.service_id', '=', 's.id')
            ->whereNull('c.deleted_at');
        if ($limit !== false) {
            $couriers = $query->orderBy('created_at', 'desc')->paginate($limit);
        } else {
            $couriers = $query->orderBy('created_at', 'desc')->get();
        }
        return $couriers;
    }

    /**
     * 根据courier_id获取快递员信息
     *
     * Date: 2018/06/10
     * @author Tenny
     * @param $courier_id
     * @param $limit
     * @return \Illuminate\Database\Eloquent\Builder
     * @throws GeneralException
     */
    public function getCourierDetails($courier_id, $limit = 15)
    {
        $courierInfo = \DB::table('courier as c')
            ->select(['c.id', 'c.name', 'c.phone', 'c.work_number', 'c.reg_type', 'c.created_at', 'c.jurisdiction', 'c.is_authentication', 'c.delivery_commission', 'c.send_commission', 'c.send_commission', 'c.income', 'c.delivery_num', 'c.send_num', 's.designation as service_name', 'a.heads_path', 'a.tails_path'])
            ->leftJoin('service as s', 's.id', '=', 'c.service_id')
            ->leftJoin('user_authentication as a', 'a.uid', '=', 'c.id')
            ->where('c.id', '=', $courier_id)
            ->first();
        if ($limit !== false) {
            $userLogRepository = new UserLogRepository;
            $admin_log = $userLogRepository->getLog($courier_id, false, 0, false, $limit);
            $courierInfo->courier_log = $admin_log;
        }
        return $courierInfo;
    }

    /**
     * 根据courier ID 获取快递员详情列表
     * Date: 2018/05/21
     * @param $courier_id
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function CourierInfo($courier_id)
    {
        $redis_key = sprintf('COURIER_INFO_V1_%d', $courier_id);
        if (!Redis::exists($redis_key)) {
            $courierInfo = \DB::table('courier as c')
                ->select(['c.id', 'c.avatar', 'c.name', 'c.phone', 's.designation as service_name', 'c.is_authentication', 'c.jurisdiction'])
                ->leftJoin('service as s', 's.id', '=', 'c.service_id')
                ->where('c.id', '=', $courier_id)
                ->first();
            Redis::setex($redis_key, self::REDIS_EXPIRES_FOR_DAY, json_encode($courierInfo));
        } else {
            $res = Redis::get($redis_key);
            $courierInfo = json_decode($res);
        }
        return $courierInfo;
    }

    protected function serialization(array $attributes)
    {
        // TODO: Implement serialization() method.
    }
}